﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.ADF.Connection;
using System.Collections;
using System.Xml;

/***************************************
 * Class Name:  IMSResponse
 * Description: ArcIMS 响应信息
 ***************************************/

/// <summary>
/// 对 ArcIMS 应用服务器发送请求
/// 所获得的服务器的响应信息
/// </summary>
public class IMSResponse
{
	public IMSResponse()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}


    public string[] getServiceNameList(string sAxlText, ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection serverConnection)
    {
        string[] strServiceName;

        // 声明一个 XML 的服务响应对象，即 ArcXML
        System.Xml.XmlDocument axlResponse = new System.Xml.XmlDocument();
        axlResponse.LoadXml(serverConnection.Send(sAxlText));

        System.Xml.XmlElement root = axlResponse.DocumentElement;
        System.Xml.XmlNodeList myNodeList = root.GetElementsByTagName("SERVICE");

        if (myNodeList.Count > 0)
        {
            strServiceName = new string[myNodeList.Count];
            for (int i = 0; i < myNodeList.Count; i++)
            {
                System.Xml.XmlNode xn = myNodeList.Item(i);
                strServiceName[i] = xn.Attributes["name"].InnerXml;
            }
            return strServiceName;
        }
        else
        {
            return null;
        }

    }


    //
    // 获得地图或图例地址
    //
    public string GetImageResponse(string sAxlText, ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection serverConnection, string sFlag)
    {
        string sImageUrl;

        // 声明一个 XML 的服务响应对象，即 ArcXML
        System.Xml.XmlDocument axlResponse = new System.Xml.XmlDocument();
        axlResponse.LoadXml(serverConnection.Send(sAxlText));

        if (sFlag == "map" && axlResponse.GetElementsByTagName("OUTPUT").Count == 1)
        {
            System.Xml.XmlNodeList xmlNodeList = axlResponse.GetElementsByTagName("OUTPUT");
            // 获取这个节点的 url 属性值
            sImageUrl = xmlNodeList[0].Attributes["url"].Value;
        }
        else if (sFlag == "legend" && axlResponse.GetElementsByTagName("LEGEND").Count == 1)
        {
            System.Xml.XmlNodeList xmlNodeList = axlResponse.GetElementsByTagName("LEGEND");
            // 获取这个节点的 url 属性值
            sImageUrl = xmlNodeList[0].Attributes["url"].Value;
        } 
        else 
        {
            sImageUrl = "";
        }

        return sImageUrl;
    }

    public string[] GetImagePrintResponse(string sAxlText, ESRI.ArcGIS.ADF.Connection.IMS.TCPConnection serverConnection)
    {
        string[] sImageUrl = new string[2];
        string sMapUrl;
        string sLegendUrl;

        System.Xml.XmlDocument axlResponse = new System.Xml.XmlDocument();
        axlResponse.LoadXml(serverConnection.Send(sAxlText));

        if (axlResponse.GetElementsByTagName("OUTPUT").Count == 1)
        {
            System.Xml.XmlNodeList nodeOutput = axlResponse.GetElementsByTagName("OUTPUT");
            sMapUrl = nodeOutput[0].Attributes["url"].Value;
            sImageUrl[0] = sMapUrl;
        }
        if (axlResponse.GetElementsByTagName("LEGEND").Count == 1)
        {
            System.Xml.XmlNodeList nodeLegend = axlResponse.GetElementsByTagName("LEGEND");
            sLegendUrl = nodeLegend[0].Attributes["url"].Value;
            sImageUrl[1] = sLegendUrl;
        }

        return sImageUrl;
    }

}
